/* 
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
 */
#include "stdafx.h"
#include "C_矢量插座.h"
#include "C_数值插座.h"
#include "C_物体插座.h"
#include "通用插座.h"

#include "list/list矢量插座.h"
#include "list/list数值插座.h"
#include "材质纹理插座.h"
#include "几何插座.h"

#include "节点/节点树.h"


using namespace std;



C_Vec2插座::C_Vec2插座(u16string name) :C_插座基类(name, E_值类型::e_Type_Vec2) {
	m_Value = {};
}

C_Vec2插座::~C_Vec2插座() {
}

void* C_Vec2插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Vec2) {
			return dynamic_cast<C_Vec3插座*>(socket)->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_F32) {
			auto& data = DEF_F32插座数据(socket, 0);
			vec2_set(m_Value, data);
		}
		else if (socket->m_Type == E_值类型::e_Type_F32_1D) {
			auto& data = *DEF_F32插座_1D数据(socket, 0);
			if (data.count) {
				m_Value = { data.ptr_userData[0], data.ptr_userData[0] };
			}
			else {
				m_Value = { 0,0 };
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3) {
			auto& data = DEF_Vec3插座数据(socket);
			m_Value = { float32(data.x), float32(data.y) };
		}
		else {
			//m_Value
		}
	}
	return &m_Value;
}

void C_Vec2插座::f_setData(void* data, uint8 线程ID) {
	m_Value = *((vec2*)data);
}

std::string C_Vec2插座::f_构建变量代码(int32 loc) {
	std::string code;

	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Vec2) {
			m_Code = socket->f_getCode(0);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_vec2";

		auto Type = socket->m_Type;
		if (socket->m_Type == E_值类型::e_Type_网格自定义属性) {
			Type = DEF_网格自定义属性插座数据(socket).m_Type;
		}


		auto* rt = m_pNode->f_getThis节点树();
		switch (rt->m_树类型) {
			case E_节点树类型::e_type_几何着色节点树:
			case E_节点树类型::e_type_并行节点树:
			case E_节点树类型::e_type_着色节点树: {
				switch (Type) {
					case E_值类型::e_Type_F32:
					case E_值类型::e_Type_Vec3:
					case E_值类型::e_Type_Vec4: {
						code += "vec2 " + m_Code + " = " + "vec2(" + socket->f_getCode(loc) + ");\n";
						break;
					}
					//case E_值类型::e_Type_网格自定义属性: {
					//	break;
					//}
				}
				break;
			}
			default:
				code += "vec2 " + m_Code + ";\n";
				switch (socket->m_Type) {
					case e_Type_Vec3: {
						code += m_Code + " = " + "vec_3to2(" + socket->f_getCode(loc) + ");\n";
						break;
					}
					case e_Type_Vec4: {
						code += m_Code + " = " + "vec_4转2(" + socket->f_getCode(loc) + ");\n";
						break;
					}
					case e_Type_iVec2: 
					case e_Type_iVec3: {
						code += m_Code + " = " + DEF_S(_Vec2)"(" + socket->f_getCode(loc) + ");\n";
						break;
					}
					case E_值类型::e_Type_Vec2_1D: {
						code += m_Code + " = " + socket->f_getCode(loc) + "->count ? " + socket->f_getCode(loc) + "->ptr_userData[0] : {0,0};\n";
						break;
					}
					case E_值类型::e_Type_Vec3_1D: {
						code += "if(" + socket->f_getCode(loc) + "->count){\n";
						code += m_Code + " = {" + socket->f_getCode(loc) + "->ptr_userData[0].x, " + socket->f_getCode(loc) + "->ptr_userData[0].y};\n";
						code += "} else {\n";
						code += m_Code + " = {0,0};\n";
						code += "}\n";
						break;
					}
					case E_值类型::e_Type_网格自定义属性: {
						break;
					}
				}
				break;
		}
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_Vec2插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_网格自定义属性:
		case E_值类型::e_Type_Vec4:
		case E_值类型::e_Type_Vec3:
		case E_值类型::e_Type_Vec2:
		case E_值类型::e_Type_iVec2:
		case E_值类型::e_Type_iVec3:
		case E_值类型::e_Type_F32_1D:
		case E_值类型::e_Type_Vec2_1D:
		case E_值类型::e_Type_Vec3_1D:
		case E_值类型::e_插座Type_引用数据:
			return true;
	}
	return false;
}

void C_Vec2插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	fread(&m_Value, sizeof(vec2), 1, f);
}

void C_Vec2插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
	fwrite(&m_Value, sizeof(vec2), 1, f);
}

void C_Vec2插座::f_upGPU块(uint8 线程ID, bool 更新) {
	C_插座基类::f_upGPU块(线程ID, 更新);

	if (m_GPU缓存.m_Mem.m_数量) {
		f_buf_Vec2_at(m_GPU缓存) = m_Value;
	}
}






C_Vec3插座::C_Vec3插座(u16string name, vec3* mapVal, E_值类型 type) :C_插座基类(name, type) {
	m_默认值 = {};
	m_转换值 = {};

	m_Value = &m_默认值;
}

C_Vec3插座::~C_Vec3插座() {
}

void C_Vec3插座::f_update(C_插座基类 * 连接到的输入节点, C_节点基类 * node) {

}

void* C_Vec3插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		switch (socket->m_Type) {	
			case E_值类型::e_Type_RGB:
			case E_值类型::e_Type_Vec3: {
				return socket->f_getData(0);
			}
			case E_值类型::e_Type_F32_1D: {
				const auto& data = *DEF_F32插座_1D数据(socket);
				if (data.count) m_转换值 = vec3{ (data.ptr_userData[0]), data.ptr_userData[0], data.ptr_userData[0]};
				break;
			}
			case E_值类型::e_Type_Vec2_1D: {
				const auto& data = *DEF_Vec2插座_1D数据(socket);
				if (data.count) m_转换值 = { data.ptr_userData[0].x, data.ptr_userData[0].y };
				break;
			}
			case E_值类型::e_Type_Vec3_1D: {
				const auto& data = *DEF_Vec3插座_1D数据(socket);
				if (data.count) m_转换值 = data.ptr_userData[0];
				break;
			}
			case E_值类型::e_Type_F32: {
				const auto& data = DEF_F32插座数据(socket, 0);
				vec3_填充(&m_转换值, data);
				break;
			}
			case E_值类型::e_Type_Vec2: {
				const auto& data = DEF_Vec2插座数据(socket);
				m_转换值 = { float32(data.x), float32(data.y), 0 };
				break;
			}
			case E_值类型::e_Type_I32: {
				const auto data = DEF_I32插座数据(socket, 0);
				vec3_填充(&m_转换值, data);
				break;
			}
			case E_值类型::e_Type_Object: {
				auto* data = DEF_物体插座数据(socket, 0);
				m_转换值 = f_ob_get全局坐标(data, 0);
				break;
			}
			default:
				break;
		}
		m_Value = &m_转换值;
	}
	else if (!m_映射指针) {
		m_Value = &m_默认值;
	}
	return m_Value;
}

void C_Vec3插座::f_setData(void* data, uint8 线程ID) {
	C_插座基类::f_setData(data);
	if (data) {
		m_转换值 = *((vec3*)data);
		m_Value = &m_转换值;
		m_映射指针 = true;
	}
	else {
		m_Value = &m_默认值;
		m_映射指针 = false;
	}
}

std::string C_Vec3插座::f_构建变量代码(int32 loc) {
	std::string code;

	C_插座基类* socket = f_getLinkOutSocket(loc);
	
	if (socket) {
		if (socket->m_Type == e_Type_Vec3) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_vec3";
		code = socket->f_getCode(loc);

		auto Type = socket->m_Type;
		if (socket->m_Type == E_值类型::e_Type_网格自定义属性) {
			Type = DEF_网格自定义属性插座数据(socket).m_Type;
		}

		auto* rt = m_pNode->f_getThis节点树();
		switch (rt->m_树类型) {
			case E_节点树类型::e_type_几何着色节点树:
			case E_节点树类型::e_type_并行节点树:
			case E_节点树类型::e_type_着色节点树: {
				switch (Type) {
					case E_值类型::e_Type_uVec2:
					case E_值类型::e_Type_Vec2: {
						code = "vec3 " + m_Code + " = vec3(" + code + ", 0);\n";
						break;
					}
					case E_值类型::e_Type_颜色:
					case E_值类型::e_Type_F32:
					case E_值类型::e_Type_I32:
					case E_值类型::e_Type_UI32:
					case E_值类型::e_Type_uVec3:
					case E_值类型::e_Type_uVec4:
					case E_值类型::e_Type_Vec4: {
						code = "vec3 " + m_Code + " = vec3(" + code + ");\n";
						break;
					}
				}
				break;
			}
			default:
				switch (socket->m_Type) {
					case E_值类型::e_Type_颜色:
				case E_值类型::e_Type_F32:
				case E_值类型::e_Type_UI32:
				case E_值类型::e_Type_I32:
				case E_值类型::e_Type_Vec4:
				case E_值类型::e_Type_uVec2: 
				case E_值类型::e_Type_iVec2:
				case E_值类型::e_Type_iVec3: {
					code = "vec3 " + m_Code + " = " + DEF_S(_Vec3)"(" + code + ");\n";
					break;
				}
				case E_值类型::e_Type_F32_1D: 
				case E_值类型::e_Type_Vec2_1D:
				case E_值类型::e_Type_Vec3_1D:
				case E_值类型::e_Type_Vec4_1D:
					code = "if(" + code + "->count > 0) " + m_Code + " = " + DEF_S(_Vec3)"(" + code + "->ptr_userData[0]);\n";
					code += "else " + m_Code + " = {};\n";
					code = "vec3 " + m_Code + ";\n" + code;
					break;

				case E_值类型::e_Type_纹理: {
					switch (rt->m_树类型) {
					case E_节点树类型::e_type_着色节点树: {
						std::string 纹理坐标 = m_Code;
						code = "vec2 " + 纹理坐标 + ";\n";

						switch (C_节点树::G_编译优化) {
						case 0: {

							break;
						}
						default:
							break;
						}
						S_纹理* tex = DEF_纹理插座数据(socket, 0);

						if (tex->m_TexID >= 0) {
							m_Code = "texture(Ts[nonuniformEXT(" + f_整数值转字符串(tex->m_TexID) + ")], " + m_Code + ").rgb";

							code += "switch (i32_GParam.int32Ptr[" + f_整数值转字符串(tex->m_纹理坐标计算类型) + "]) {\n";
							code += "case 0: " + 纹理坐标 + " = bound_uv.xz; break; \n";
							code += "case 1: " + 纹理坐标 + " = f_uv_spherical(bound_uv, boundSize).xy; break; \n";
							code += "case 2: " + 纹理坐标 + " = f_uv_spherical(bound_uv, boundSize).yx; break; \n";
							code += "case 3: " + 纹理坐标 + " = f_uv_spherical(bound_uv, boundSize).xx; break; \n";
							code += "}\n";
						}
						else {
							m_Code = "vec3_GParam.vec3Ptr[" + f_整数值转字符串(m_GPU缓存.m_Mem.m_偏移) + "]";
						}
					}
					default:
						break;
					}

					break;
				}
				default:
					m_Code = "";
					code = "";
					break;
				}
			break;
		}
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_Vec3插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_网格自定义属性:

		case E_值类型::e_Type_iVec4:
		case E_值类型::e_Type_iVec3:
		case E_值类型::e_Type_iVec2:

		case E_值类型::e_Type_uVec2:
		case E_值类型::e_Type_uVec3:

		case E_值类型::e_Type_Vec4:
		case E_值类型::e_Type_Vec3:
		case E_值类型::e_Type_Vec2:
		case E_值类型::e_Type_Vec2_1D:
		case E_值类型::e_Type_Vec3_1D:
		case E_值类型::e_Type_iVec3_1D:
		case E_值类型::e_Type_Vec3_Array1D:
		case E_值类型::e_Type_UI32:
		case E_值类型::e_Type_I32:
		case E_值类型::e_Type_F32:
		case E_值类型::e_Type_F32_1D:
		case E_值类型::e_Type_纹理:
		case E_值类型::e_Type_颜色:
		case E_值类型::e_Type_Array:
			return true;
		default:
			break;
	}
	return false;
}

void C_Vec3插座::f_upGPU块(uint8 线程ID, bool 更新) {
	C_插座基类::f_upGPU块(线程ID, 更新);

	if (m_GPU缓存.m_Mem.m_数量) {
		f_buf_Vec3_at(m_GPU缓存) = *m_Value;
	}
}


void C_Vec3插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	fread(m_Value, sizeof(vec3), 1, f);
}

void C_Vec3插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
	fwrite(m_Value, sizeof(vec3), 1, f);
}

void C_Vec3插座::f_Copy(const C_插座基类* socket) {
	C_插座基类::f_Copy(socket);
	const C_Vec3插座* s = dynamic_cast<const C_Vec3插座*>(socket);
	if (s) {
		(*m_Value) = *s->m_Value;
	}
}








C_Vec4插座::C_Vec4插座(u16string name, E_值类型 type) :C_插座基类(name, type) {
	m_默认值 = { 0,0,0,1 };
	m_转换值 = m_默认值;
	m_Value = &m_默认值;
}

C_Vec4插座::~C_Vec4插座() {
}

void* C_Vec4插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		switch (socket->m_Type) {
			case E_值类型::e_Type_Vec4: {
				return socket->f_getData(0);
			}
			case E_值类型::e_Type_颜色: {
				auto v = DEF_颜色插座数据(socket);
				m_转换值 = _Vec4(v);
				break;
			}
			case E_值类型::e_Type_F32: {
				float32 v = DEF_F32插座数据(socket, 0);
				m_转换值 = _Vec4(v);
				break;
			}
			default:
				break;
		}
		m_Value = &m_转换值;
	}
	else if (!m_映射指针) {
		m_Value = &m_默认值;
	}
	return m_Value;
}

void C_Vec4插座::f_setData(void* data, uint8 ID) {
	C_插座基类::f_setData(data, ID);
	if (data) {
		*m_Value = *((vec4*)data);
	}
	else {
		m_Value = &m_默认值;
	}
}

std::string C_Vec4插座::f_构建变量代码(int32 loc) {
	C_插座基类* socket = f_getLinkOutSocket(loc);

	std::string code;
	if (socket) {
		if (socket->m_Type == e_Type_Vec4) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_vec4";
		
		auto Type = socket->m_Type;
		if (socket->m_Type == E_值类型::e_Type_网格自定义属性) {
			Type = DEF_网格自定义属性插座数据(socket).m_Type;
		}

		auto* rt = m_pNode->f_getThis节点树();
		switch (rt->m_树类型) {
			case E_节点树类型::e_type_并行节点树:
			case E_节点树类型::e_type_着色节点树: {
				switch (Type) {
					case e_Type_iVec2:
					case e_Type_uVec2:
					case e_Type_Vec2: code += "vec4 " + m_Code + " = " + "vec4(" + socket->f_getCode(loc) + ", 0, 0);\n"; break;

					case e_Type_iVec3:
					case e_Type_uVec3:
					case e_Type_Vec3: code += "vec4 " + m_Code + " = " + "vec4(" + socket->f_getCode(loc) + ", 0);\n"; break;

					case E_值类型::e_Type_颜色:
					case E_值类型::e_Type_F32:
					case E_值类型::e_Type_I32:
					case E_值类型::e_Type_UI32:
					case e_Type_iVec4:
					case e_Type_uVec4:
					case e_Type_Vec4: code += "vec4 " + m_Code + " = " + "vec4(" + socket->f_getCode(loc) + ");\n"; break;
				}
				break;
			}
			default: {
				code = "vec4 " + m_Code + ";\n";
				switch (socket->m_Type) {
					case E_值类型::e_Type_颜色: {
						code += m_Code + " = " + DEF_S(_Vec4) + "(" + socket->f_getCode(loc) + ");\n";
						break;
					}
					case e_Type_Vec3: {
						code += m_Code + " = " + "vec_3转4(" + socket->f_getCode(loc) + ", 0);\n";
						break;
					}
					case E_值类型::e_Type_Vec4_1D: {
						code += "if(" + socket->f_getCode(loc) + "->count > 0) " + m_Code + " = " + socket->f_getCode(loc) + "->ptr_userData[0];\n";
						code += "else " + m_Code + " = {};\n";
						break;
					}
				}
				break;
			}
		}
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_Vec4插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_网格自定义属性:
		case E_值类型::e_Type_颜色:
		case E_值类型::e_Type_Vec4:
		case E_值类型::e_Type_Vec3:
		case E_值类型::e_Type_Vec4_Array1D:
		case E_值类型::e_Type_Vec4_1D:
		case E_值类型::e_Type_Array:
			return true;
	}
	return false;
}

void C_Vec4插座::f_upGPU块(uint8 线程ID, bool 更新) {
	C_插座基类::f_upGPU块(线程ID, 更新);

	if (m_GPU缓存.m_Mem.m_数量) {
		f_buf_Vec4_at(m_GPU缓存) = *m_Value;
	}
}

void C_Vec4插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(&m_默认值, sizeof(vec4), 1, f);
}

void C_Vec4插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(&m_默认值, sizeof(vec4), 1, f);
}






C_iVec2插座::C_iVec2插座(u16string name, ivec2* m) :C_插座基类(name, E_值类型::e_Type_iVec2) {
	m_默认值 = {};
	if (m) {
		m_Value = m;
	}
	else {
		m_Value = &m_默认值;
	}
}

C_iVec2插座::~C_iVec2插座() {
}

S_GPU内存块& C_iVec2插座::f_getGPU块(uint8 线程ID, bool 更新) {
	C_插座基类::f_getGPU块(线程ID, 更新);
	if (!m_GPU缓存.m_Buf) {
		m_GPU缓存 = f_bm_alloc(f_buf_getGlobal().m_iVec2动态属性, 1);
	}

	if (更新) {
		ivec2 data = DEF_iVec2插座数据(this);
		f_gbuf_fill(m_GPU缓存, &data, 0, 1);
	}
	return m_GPU缓存;
}

S_GPU内存块& C_iVec2插座::f_downGPU块(uint8 线程ID, bool 更新) {
	ivec2& data = DEF_iVec2插座数据(this);
	if (m_GPU缓存.m_Buf) {
		data = f_buf_iVec2_at(m_GPU缓存);
	}
	return m_GPU缓存;
}

void* C_iVec2插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_iVec2) {
			return dynamic_cast<C_iVec2插座*>(socket)->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3) {
			auto& data = DEF_Vec3插座数据(socket);
			*m_Value = { int32(data.x), int32(data.y) };
		}
	}
	return m_Value;
}

void C_iVec2插座::f_setData(void * data, int64 ID) {
	(*m_Value) = *((ivec2*)data);
}

std::string C_iVec2插座::f_构建变量代码(int32 loc) {
	std::string code;

	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == e_Type_iVec2) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_vec3";
		code = socket->f_getCode(loc);

		auto Type = socket->m_Type;
		if (socket->m_Type == E_值类型::e_Type_网格自定义属性) {
			Type = DEF_网格自定义属性插座数据(socket).m_Type;
		}

		auto* rt = m_pNode->f_getThis节点树();
		switch (rt->m_树类型) {
			case E_节点树类型::e_type_几何着色节点树:
			case E_节点树类型::e_type_并行节点树: 
			case E_节点树类型::e_type_着色节点树: {
				switch (Type) {
					case E_值类型::e_Type_F32:
					case E_值类型::e_Type_I32:
					case E_值类型::e_Type_UI32:
					//	code = "vec3 " + m_Code + " = vec3(" + code + ", " + code + ", " + code + ");\n";
					//	break;
					case e_Type_uVec2:
					case e_Type_uVec3:
					case e_Type_uVec4:
					case e_Type_iVec2:
					case e_Type_iVec3:
					case e_Type_iVec4:
					case e_Type_Vec2:
					case e_Type_Vec4: {
						code = "ivec2 " + m_Code + " = ivec2(" + code + ");\n";
						break;
					}
				}
				break;
			}
			default:
				switch (socket->m_Type) {
					case E_值类型::e_Type_F32:
						code = "ivec2 " + m_Code + " = {" + code + ", " + code + "};\n";
						break;
					case E_值类型::e_Type_I32:
						code = "ivec2 " + m_Code + " = {(int32)" + code + ", (int32)" + code + "};\n";
						break;
					case e_Type_iVec3:
					case e_Type_Vec2:
					case e_Type_Vec3:
					case e_Type_Vec4: {
						code = "ivec2 " + m_Code + " = {(int32)((" + code + ").x), (int32)((" + code + ").y) };\n";
						break;
					}
					case E_值类型::e_Type_I8_1D:
					case E_值类型::e_Type_I32_1D:
					case E_值类型::e_Type_F32_1D:
						code += "if(" + code + "->count > 0) " + m_Code + " = {(int32)(" + code + "->ptr_userData[0]), (int32)(" + code + "->ptr_userData[0]) };\n";
						code += "else " + m_Code + " = {};\n";

						code = "ivec2 " + m_Code + ";\n" + code;
						break;
					case E_值类型::e_Type_Vec2_1D:
					case E_值类型::e_Type_Vec3_1D:
					case E_值类型::e_Type_Vec4_1D:
						code += "if(" + code + "->count > 0) " + m_Code + " = {(int32)(" + code + "->ptr_userData[0].x), (int32)(" + code + "->ptr_userData[0].y) };\n";
						code += "else " + m_Code + " = {};\n";

						code = "ivec2 " + m_Code + ";\n" + code;
						break;
					default:
						m_Code = "";
						code = "";
						break;
				}
				break;
		}
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_iVec2插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_网格自定义属性:

		case E_值类型::e_Type_F32:
		case E_值类型::e_Type_I8:
		case E_值类型::e_Type_I32:
		case E_值类型::e_Type_UI32:
		case E_值类型::e_Type_Vec2:
		case E_值类型::e_Type_Vec3:
		case E_值类型::e_Type_Vec4:
		case E_值类型::e_Type_iVec3:
		case E_值类型::e_Type_iVec2:
		case E_值类型::e_Type_uVec2:
		case E_值类型::e_Type_uVec3:
		case E_值类型::e_Type_uVec4:
		//case E_值类型::e_Type_Array:
			return true;
	}
	return false;
}

void C_iVec2插座::f_upGPU块(uint8 线程ID, bool 更新) {
	C_插座基类::f_upGPU块(线程ID, 更新);

	if (m_GPU缓存.m_Mem.m_数量) {
		f_buf_iVec2_at(m_GPU缓存) = *m_Value;
	}
}

void C_iVec2插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(m_Value, sizeof(ivec2), 1, f);
}

void C_iVec2插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(m_Value, sizeof(ivec2), 1, f);
}









C_uVec2插座::C_uVec2插座(u16string name) : C_插座基类(name, E_值类型::e_Type_uVec2) {
	m_默认值 = {};
	m_转换值 = {};
	m_Value = &m_默认值;
}

C_uVec2插座::~C_uVec2插座() {
}

void* C_uVec2插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_uVec2) {
			return socket->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3_Array1D) {
			const auto& data = *DEF_Vec3_Array1D插座数据(socket, 0);
			if (data.size()) {
				m_转换值.x = data[0].x;
				m_转换值.y = data[0].y;
			}
			else {
				m_转换值 = {};
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3) {
			const auto& data = DEF_Vec3插座数据(socket, 0);
			m_转换值.x = data.x;
			m_转换值.y = data.y;
		}
		else if (socket->m_Type == E_值类型::e_Type_F32) {
			auto data = DEF_F32插座数据(socket, 0);
			m_转换值.x = data;
			m_转换值.y = data;
		}
		else if (socket->m_Type == E_值类型::e_Type_F32_Array1D) {
			auto& data = *DEF_F32_Array1D插座数据(socket, 0);

			m_转换值.x = data[0];
			m_转换值.y = data[0];
		}
		else {
			//m_Value
		}

		m_Value = &m_转换值;
	}
	else if (!m_映射指针) {
		m_Value = &m_默认值;
	}
	return m_Value;
}

void C_uVec2插座::f_setData(void* data, uint8 线程ID) {
	if (data) {
		m_转换值 = *((uvec2*)data);

		m_Value = &m_转换值;
		m_映射指针 = true;
	}
	else {
		m_Value = &m_默认值;
		m_映射指针 = false;
	}
}

std::string C_uVec2插座::f_构建变量代码(int32 loc) {
	std::string code;

	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == e_Type_uVec2) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_uvec2";


		switch (socket->m_Type) {
		case e_Type_Vec3: {
			code = socket->f_getCode(loc);
			code = "uvec2 " + m_Code + " = " + "vec_f3转u2(" + code + ");\n";
			break;
		}
		default:
			break;
		}
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_uVec2插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_uVec2:
		case E_值类型::e_Type_Vec3:
		case E_值类型::e_Type_Vec3_Array1D:
		//case E_值类型::e_Type_uVec2:
		case E_值类型::e_Type_F32:
		case E_值类型::e_Type_F32_Array1D:
			return true;
	}
	return false;
}

void C_uVec2插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	fread(m_Value, sizeof(uvec2), 1, f);
}

void C_uVec2插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
	fwrite(m_Value, sizeof(uvec2), 1, f);
}





C_iVec3插座::C_iVec3插座(u16string name) :C_插座基类(name, E_值类型::e_Type_iVec3) {
	m_默认值 = {};
	m_转换值 = {};

	m_Value = &m_默认值;
}

C_iVec3插座::~C_iVec3插座() {
}


void * C_iVec3插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_iVec3) {
			return dynamic_cast<C_iVec3插座*>(socket)->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec2) {
			auto& val = DEF_Vec2插座数据(socket, 0);
			m_转换值.x = val.x;
			m_转换值.y = val.y;
		}
		else if (socket->m_Type == E_值类型::e_Type_iVec2) {
			auto& val = DEF_iVec2插座数据(socket, 0);
			m_转换值.x = val.x;
			m_转换值.y = val.y;
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3) {
			auto& val = DEF_Vec3插座数据(socket);
			m_转换值.x = val.x;
			m_转换值.y = val.y;
			m_转换值.z = val.z;
		}
		else if (socket->m_Type == E_值类型::e_Type_F32) {
			auto& val = DEF_F32插座数据(socket);
			m_转换值.x = val;
			m_转换值.y = val;
			m_转换值.z = val;
		}
		else if (socket->m_Type == E_值类型::e_Type_I32) {
			auto& val = DEF_I32插座数据(socket);
			m_转换值.x = val;
			m_转换值.y = val;
			m_转换值.z = val;
		}
		else if (socket->m_Type == E_值类型::e_Type_UI32) {
			auto& val = DEF_UI32插座数据(socket);
			m_转换值.x = val;
			m_转换值.y = val;
			m_转换值.z = val;
		}
		else if (socket->m_Type == E_值类型::e_Type_I8) {
			auto& val = DEF_I8插座数据(socket, 0);
			m_转换值.x = val;
			m_转换值.y = val;
			m_转换值.z = val;
		}
		else {

		}
		m_Value = &m_转换值;
	}
	else if(!m_映射指针) {
		m_Value = &m_默认值;
	}
	return m_Value;
}

void C_iVec3插座::f_setData(void * data, uint8 线程ID) {
	C_插座基类::f_setData(data, 线程ID);
	if (data) {
		(*m_Value) = *((ivec3*)data);
	}
	else {
		m_Value = &m_默认值;
	}
}

std::string	C_iVec3插座::f_构建变量代码(int32 loc) {
	std::string code;

	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == e_Type_iVec3) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_ivec3";
		code = socket->f_getCode(loc);

		auto Type = socket->m_Type;
		if (socket->m_Type == E_值类型::e_Type_网格自定义属性) {
			Type = DEF_网格自定义属性插座数据(socket).m_Type;
		}


		auto* rt = m_pNode->f_getThis节点树();
		switch (rt->m_树类型) {
			case E_节点树类型::e_type_并行节点树:
			case E_节点树类型::e_type_着色节点树: {
				switch (Type) {
					case E_值类型::e_Type_F32:
					case e_Type_Vec4: {
						code = "ivec3 " + m_Code + " = ivec3(" + code + ");\n";
						break;
					}
				}
				break;
			}
			default:
				switch (socket->m_Type) {
					case E_值类型::e_Type_F32:
						code = "ivec3 " + m_Code + " = {" + code + ", " + code + ", " + code + "};\n";
						break;
					case E_值类型::e_Type_I32:
						code = "ivec3 " + m_Code + " = {(int32)" + code + ", (int32)" + code + ", (int32)" + code + "};\n";
						break;

					case E_值类型::e_Type_Vec2:
					case E_值类型::e_Type_iVec2:
					case E_值类型::e_Type_uVec2:
					case E_值类型::e_Type_uVec3:
					case e_Type_Vec3:
					case e_Type_Vec4: {
						code = "ivec3 " + m_Code + " = " + DEF_S(_iVec3) + "(" + code + ");\n";
						break;
					}
					//case e_Type_iVec2: 
					//case e_Type_Vec2: {
					//	code = "ivec3 " + m_Code + " = {(int32)((" + code + ").x), (int32)((" + code + ").y) };\n";
					//	break;
					//}
					case E_值类型::e_Type_I8_1D:
					case E_值类型::e_Type_I32_1D:
					case E_值类型::e_Type_F32_1D:
					case E_值类型::e_Type_Vec2_1D:
					case E_值类型::e_Type_Vec3_1D:
					case E_值类型::e_Type_Vec4_1D:
						code = "if(" + code + "->count > 0) " + m_Code + " = " + DEF_S(_iVec3)"(" + code + "->ptr_userData[0]);\n";
						code += "else " + m_Code + " = {};\n";

						code = "ivec3 " + m_Code + ";\n" + code;
						break;
					default:
						m_Code = "";
						code = "";
						break;
				}
				break;
		}
		//m_pNode->m_Code += code;
		//m_pNode->m_ReleaseCode += "f_JIT_Fun_vec3_free(" + m_Code + ");\n";
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_iVec3插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_网格自定义属性:

		case E_值类型::e_Type_F32:
		case E_值类型::e_Type_I8:
		case E_值类型::e_Type_I32:
		case E_值类型::e_Type_UI32:
		case E_值类型::e_Type_Vec2:
		case E_值类型::e_Type_Vec3:
		case E_值类型::e_Type_Vec4:
		case E_值类型::e_Type_Vec4_Array1D:
		case E_值类型::e_Type_Vec3_1D:
		case E_值类型::e_Type_Vec4_1D:
		case E_值类型::e_Type_iVec2:
		case E_值类型::e_Type_iVec3:
		case E_值类型::e_Type_uVec2:
		case E_值类型::e_Type_uVec3:
		case E_值类型::e_Type_iVec3_1D:
		case E_值类型::e_Type_Array:
			return true;
	}
	return false;
}

void C_iVec3插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(m_Value, sizeof(ivec3), 1, f);
}

void C_iVec3插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(m_Value, sizeof(ivec3), 1, f);
}




C_uVec3插座::C_uVec3插座(u16string name) :C_插座基类(name, E_值类型::e_Type_uVec3) {
	m_Value = {};
}

void * C_uVec3插座::f_getData(uint8 插座) {
	C_插座基类* socket = f_getLinkOutSocket(插座);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_uVec3) {
			return dynamic_cast<C_iVec3插座*>(socket)->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_iVec3) {
			const auto v = DEF_iVec3插座数据(socket);
			vec3_cpy(m_Value, v);
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3) {
			const auto v = DEF_Vec3插座数据(socket, 0);
			vec3_cpy(m_Value, v);
		}
		else if (socket->m_Type == E_值类型::e_Type_F32) {
			m_Value = _uVec3(DEF_F32插座数据(socket));
		}
		else if (socket->m_Type == E_值类型::e_Type_I8) {
			m_Value = _uVec3(DEF_I8插座数据(socket));
		}
		else if (socket->m_Type == E_值类型::e_Type_I32) {
			m_Value = _uVec3(DEF_I32插座数据(socket));
		}
		else if (socket->m_Type == E_值类型::e_Type_UI32) {
			m_Value = _uVec3(DEF_UI32插座数据(socket));
		}
	}
	return &m_Value;
}

void C_uVec3插座::f_setData(void* data, uint8 线程ID) {
	m_Value = *((uvec3*)data);
}

std::string C_uVec3插座::f_构建变量代码(int32 loc) {
	std::string code;

	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == e_Type_uVec3) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_uvec3";

		auto Type = socket->m_Type;
		if (socket->m_Type == E_值类型::e_Type_网格自定义属性) {
			Type = DEF_网格自定义属性插座数据(socket).m_Type;
		}

		switch (socket->m_Type) {
			case e_Type_I32:
			case e_Type_UI32:
			case e_Type_F32:
			case e_Type_iVec3:
			case e_Type_uVec2:
			case e_Type_uVec3:
			case e_Type_Vec3: {
				code += "uvec3 " + m_Code + " = " + DEF_S(_uVec3)"(" + socket->f_getCode(loc) + ");\n";
				break;
			}
			case e_Type_Vec3_1D: {
				code += "uvec3 " + m_Code + " = " + DEF_S(_uVec3)"(" + socket->f_getCode(loc) + "->ptr_userData[0]);\n";
				break;
			}
		}
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_uVec3插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_F32:
		case E_值类型::e_Type_I8:
		case E_值类型::e_Type_I32:
		case E_值类型::e_Type_UI32:
		case E_值类型::e_Type_Vec2:
		case E_值类型::e_Type_Vec3:
		case E_值类型::e_Type_Vec4:
		case E_值类型::e_Type_Vec4_Array1D:
		case E_值类型::e_Type_Vec3_1D:
		case E_值类型::e_Type_Vec4_1D:
		case E_值类型::e_Type_iVec2:
		case E_值类型::e_Type_iVec3:
		case E_值类型::e_Type_uVec2:
		case E_值类型::e_Type_uVec3:
		case E_值类型::e_Type_Array:
			return true;
	}
	return false;
}

void C_uVec3插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(&m_Value, sizeof(uvec3), 1, f);
}

void C_uVec3插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(&m_Value, sizeof(uvec3), 1, f);
}






C_iVec4插座::C_iVec4插座(u16string name) :C_插座基类(name, E_值类型::e_Type_iVec3) {
	m_默认值 = {};
	m_转换值 = {};

	m_Value = &m_默认值;
}

C_iVec4插座::~C_iVec4插座() {
}


void* C_iVec4插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_iVec3) {
			return dynamic_cast<C_iVec3插座*>(socket)->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec2) {
			auto& val = DEF_Vec2插座数据(socket, 0);
			m_转换值 = _iVec4(val);
		}
		else if (socket->m_Type == E_值类型::e_Type_iVec2) {
			auto& val = DEF_iVec2插座数据(socket, 0);
			m_转换值 = _iVec4(val);
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3) {
			auto& val = DEF_Vec3插座数据(socket);
			m_转换值 = _iVec4(val);
		}
		else if (socket->m_Type == E_值类型::e_Type_F32) {
			auto& val = DEF_F32插座数据(socket);
			m_转换值 = _iVec4(val);
		}
		else if (socket->m_Type == E_值类型::e_Type_I32) {
			auto& val = DEF_I32插座数据(socket);
			m_转换值 = _iVec4(val);
		}
		else if (socket->m_Type == E_值类型::e_Type_UI32) {
			auto& val = DEF_UI32插座数据(socket);
			m_转换值 = _iVec4(val);
		}
		else if (socket->m_Type == E_值类型::e_Type_I8) {
			auto& val = DEF_I8插座数据(socket, 0);
			m_转换值 = _iVec4(val);
		}
		else {

		}
		m_Value = &m_转换值;
	}
	else if (!m_映射指针) {
		m_Value = &m_默认值;
	}
	return m_Value;
}

void C_iVec4插座::f_setData(void* data, uint8 线程ID) {
	C_插座基类::f_setData(data, 线程ID);
	if (data) {
		(*m_Value) = *((ivec4*)data);
	}
	else {
		m_Value = &m_默认值;
	}
}

std::string	C_iVec4插座::f_构建变量代码(int32 loc) {
	std::string code;

	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == e_Type_iVec3) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_ivec4";
		code = socket->f_getCode(loc);

		auto Type = socket->m_Type;
		if (socket->m_Type == E_值类型::e_Type_网格自定义属性) {
			Type = DEF_网格自定义属性插座数据(socket).m_Type;
		}


		auto* rt = m_pNode->f_getThis节点树();
		switch (rt->m_树类型) {
			case E_节点树类型::e_type_着色节点树: {
				switch (Type) {
					case E_值类型::e_Type_F32:
					case E_值类型::e_Type_I32:
					case e_Type_Vec4: {
						code = "ivec4 " + m_Code + " = ivec4(" + code + ");\n";
						break;
					}
				}
				break;
			}
			default:
				switch (socket->m_Type) {
					case E_值类型::e_Type_F32:
					case E_值类型::e_Type_I32:
					case e_Type_Vec3:
					case e_Type_Vec4:
					case e_Type_iVec2:
					case e_Type_iVec3:
					case e_Type_Vec2: {
						code = "ivec4 " + m_Code + " = _iVec4(" + code + ");\n";
						break;
					}
					case E_值类型::e_Type_I8_1D:
					case E_值类型::e_Type_I32_1D:
					case E_值类型::e_Type_F32_1D:
					case E_值类型::e_Type_Vec2_1D:
					case E_值类型::e_Type_Vec3_1D:
					case E_值类型::e_Type_Vec4_1D:
						code = "if(" + code + "->count > 0) " + m_Code + " = " + DEF_S(_iVec4)"(" + code + "->ptr_userData[0]);\n";
						code += "else " + m_Code + " = {};\n";

						code = "ivec4 " + m_Code + ";\n" + code;
						break;
					default:
						m_Code = "";
						code = "";
						break;
				}
				break;
		}
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_iVec4插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_网格自定义属性:

		case E_值类型::e_Type_F32:
		case E_值类型::e_Type_I8:
		case E_值类型::e_Type_I32:
		case E_值类型::e_Type_UI32:
		case E_值类型::e_Type_Vec2:
		case E_值类型::e_Type_Vec3:
		case E_值类型::e_Type_Vec4:
		case E_值类型::e_Type_Vec3_1D:
		case E_值类型::e_Type_Vec4_1D:
		case E_值类型::e_Type_iVec2:
		case E_值类型::e_Type_iVec3:
		case E_值类型::e_Type_iVec4:
		case E_值类型::e_Type_iVec3_1D:
		case E_值类型::e_Type_Array:
			return true;
	}
	return false;
}

void C_iVec4插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(m_Value, sizeof(ivec3), 1, f);
}

void C_iVec4插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(m_Value, sizeof(ivec3), 1, f);
}





C_uVec4插座::C_uVec4插座(u16string name)
	:C_插座基类(name, E_值类型::e_Type_uVec4)
{
}

C_uVec4插座::~C_uVec4插座() {
}

void * C_uVec4插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_uVec4) {
			return socket->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_I8) {
			auto data = DEF_I8插座数据(socket, 0);
			m_Value.x = data;
			m_Value.y = data;
			m_Value.z = data;
			m_Value.w = data;
		}
		else if (socket->m_Type == E_值类型::e_Type_I32) {
			auto data = DEF_I32插座数据(socket);
			m_Value.x = data;
			m_Value.y = data;
			m_Value.z = data;
			m_Value.w = data;
		}
		else if (socket->m_Type == E_值类型::e_Type_F32) {
			auto data = DEF_F32插座数据(socket);
			m_Value.x = data;
			m_Value.y = data;
			m_Value.z = data;
			m_Value.w = data;
		}
		else if (socket->m_Type == E_值类型::e_Type_I8_1D) {
			auto& data = *DEF_I8插座_1D数据(socket, 0);
			if (data.count) {
				m_Value.x = data.ptr_userData[0];
				m_Value.y = m_Value.x;
				m_Value.z = m_Value.x;
				m_Value.w = m_Value.x;
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_F32_Array1D) {
			auto& data = *DEF_F32_Array1D插座数据(socket, 0);
			if (data.size()) {
				m_Value.x = data[0];
				m_Value.y = data[0];
				m_Value.z = data[0];
				m_Value.w = data[0];
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_I32_1D) {
			auto& data = *DEF_I32插座_1D数据(socket, 0);
			if (data.count) {
				m_Value.x = data.ptr_userData[0];
				m_Value.y = data.ptr_userData[0];
				m_Value.z = data.ptr_userData[0];
				m_Value.w = data.ptr_userData[0];
			}
		}

	}
	return &m_Value;
}

void C_uVec4插座::f_setData(void * data, int64 ID) {
	m_Value = *((uvec4*)data);
}

void C_uVec4插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(&m_Value, sizeof(uvec4), 1, f);
}

void C_uVec4插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(&m_Value, sizeof(uvec4), 1, f);
}














C_Mat3X3插座::C_Mat3X3插座(u16string name)
	:C_插座基类(name, E_值类型::e_Type_Mat3X3)
{
}

C_Mat3X3插座::~C_Mat3X3插座() {
}

void* C_Mat3X3插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Mat3X3) {
			return dynamic_cast<C_Mat3X3插座*>(socket)->f_getData(线程ID);
		}
	}
	return &m_Value;
}

void C_Mat3X3插座::f_setData(void * data, int64 ID) {
	m_Value = *((mat3X3*)data);
}

void C_Mat3X3插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(&m_Value, sizeof(mat3X3), 1, f);
}

void C_Mat3X3插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(&m_Value, sizeof(mat3X3), 1, f);
}










C_Matrix4X4插座::C_Matrix4X4插座(u16string name) :C_插座基类(name, E_值类型::e_Type_Mat4X4) {
	m_Value = f_mat44_identity();
}

C_Matrix4X4插座::~C_Matrix4X4插座() {
}

void* C_Matrix4X4插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Mat4X4) {
			return dynamic_cast<C_Matrix4X4插座*>(socket)->f_getData(线程ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_Mat4X4_1D) {
			this;
		}
		else if (socket->m_Type == E_值类型::e_Type_Mat4X4_Array1D) {
			auto& e = *DEF_Mat44_Array1D插座数据(socket, 0);
			if (e.size()) {
				m_Value = e[0];
			}
		}
	}
	return &m_Value;
}

void C_Matrix4X4插座::f_setData(void * data, int64 ID) {
	m_Value = *((Mat44f*)data);
}

void C_Matrix4X4插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(&m_Value, sizeof(Mat44f), 1, f);
}

void C_Matrix4X4插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(&m_Value, sizeof(Mat44f), 1, f);
}

std::string C_Matrix4X4插座::f_构建变量代码(int32 loc) {
	std::string code;
	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == e_Type_Mat4X4) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "_new_val_to_mat44";

		switch (socket->m_Type) {
			case e_Type_Mat4X4_1D: {
				code += "Mat44f " + m_Code + " = f_Mat44_array_formMat44Array(" + socket->f_getCode(loc) + ");\n";
				break;
			}
		}
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_Matrix4X4插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
	case E_值类型::e_Type_Array:
	case E_值类型::e_Type_Mat4X4:
	case E_值类型::e_Type_Mat4X4_1D:
	case E_值类型::e_Type_Mat4X4_Array1D:
		return true;
	}
	return false;
}





//C_Quat插座::C_Quat插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_Quat) {
//	m_Value.x = 0;
//	m_Value.y = 0;
//	m_Value.z = 0;
//	m_Value.w = 1;
//}
//
//C_Quat插座::~C_Quat插座() {
//}
//
//void* C_Quat插座::f_getData(uint8 线程ID) {
//	C_插座基类* socket = f_getLinkOutSocket(线程ID);
//	if (socket) {
//		if (socket->m_Type == E_值类型::e_Type_Quat) {
//			return socket->f_getData(线程ID);
//		}
//	}
//	return &m_Value;
//}
//
//void C_Quat插座::f_setData(void* data, int64 ID) {
//	m_Value = *((Quat*)data);
//}
//
//void C_Quat插座::f_读取(FILE* f) {
//	C_插座基类::f_读取(f);
//
//	fread(&m_Value, sizeof(Quat), 1, f);
//}
//
//void C_Quat插座::f_写入(FILE* f) {
//	C_插座基类::f_写入(f);
//
//	fwrite(&m_Value, sizeof(Quat), 1, f);
//}








C_线段插座::C_线段插座(std::u16string name) :C_插座基类(name, E_值类型::e_插座Type_Line) {
	m_Value = {};
}

C_线段插座::~C_线段插座() {
	C_插座基类::~C_插座基类();
}

void* C_线段插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_插座Type_Line) {
			return socket->f_getData(线程ID);
		}
		else if (socket->m_Type == E_值类型::e_插座Type_Line_Array1D) {
			//return socket->f_getData(线程ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3_Array1D) {
			auto& v3 = *DEF_Vec3_Array1D插座数据(socket);
			uint32 num = v3.size() / 2;

			if (num) {
				m_Value.begin = v3[0];
				m_Value.end = v3[1];
			}
		}
		else if (socket->m_Type == E_值类型::e_插座Type_引用数据) {
			auto* data = DEF_引用插座数据(socket, 0);
			switch (data->m_Type) {
			case E_值类型::e_插座Type_Line:
				return socket->f_getData(线程ID);

			case E_值类型::e_插座Type_Line_Array1D: {
				auto& v3 = *DEF_Vec3_Array1D插座数据(socket);
				uint32 num = v3.size() / 2;

				if (num) {
					m_Value.begin = v3[0];
					m_Value.end = v3[1];
				}
				break;
			}
			default:
				break;
			}
		}
		else {

		}
	}
	return &m_Value;
}

void C_线段插座::f_setData(void* data, int64 ID)
{
}

void C_线段插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	f_读取文件(f, &m_Value);
}

void C_线段插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
	f_写入文件(f, m_Value);
}




C_包围盒插座::C_包围盒插座(std::u16string name) :C_插座基类(name, E_值类型::e_Type_Bounding) {
	m_转换值 = { {}, {1, 1, 1} };
	m_默认值 = m_转换值;

	m_Value = &m_默认值;
}

C_包围盒插座::~C_包围盒插座() {
}

void* C_包围盒插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		switch (socket->m_Type) {
			case E_值类型::e_Type_Bounding: {
				return socket->f_getData(线程ID);
			}
			case E_值类型::e_Type_Vec3_1D: {
				const auto& data = *DEF_Vec3插座_1D数据(socket);
				if (data.count) m_转换值.pos = data.ptr_userData[0];
				break;
			}
			case E_值类型::e_Type_F32: {
				const auto& data = DEF_F32插座数据(socket, 0);
				m_转换值.size = _Vec3(data);
				break;
			}
			case E_值类型::e_Type_I32: {
				const auto data = DEF_I32插座数据(socket, 0);
				m_转换值.size = _Vec3(data);
				break;
			}
			default:
				break;
		}

		m_Value = &m_转换值;
	}
	else if (!m_映射指针) {
		m_Value = &m_默认值;
	}
	return m_Value;
}

void C_包围盒插座::f_setData(void* data, uint8 线程ID) {
	C_插座基类::f_setData(data);
	if (data) {
		m_Value = (S_Cube*)data;
	}
	else {
		m_Value = &m_默认值;
	}
}

std::string C_包围盒插座::f_构建变量代码(int32 loc) {
	std::string code;
	C_插座基类* socket = f_getLinkOutSocket(loc);

	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Bounding) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_boundings";
		auto 输入的数据 = socket->f_getCode(loc);

		auto* rt = m_pNode->f_getThis节点树();
		switch (rt->m_树类型) {
			case E_节点树类型::e_type_着色节点树: {
				switch (socket->m_Type) {
					case e_Type_Vec3: {
						code = "vec3 " + m_Code + " = vec3(" + code + ");\n";
						break;
					}
				}
				break;
			}
			default:
				code = DEF_S(S_Cube)" " + m_Code + ";\n";
				code += m_Code + ".pos = {1,1,1};\n";
				switch (socket->m_Type) {
					case e_Type_F32:
					case e_Type_I8:
					case e_Type_I32:
					case e_Type_UI32:

					case E_值类型::e_Type_Vec3:
					case E_值类型::e_Type_Vec4:
					case E_值类型::e_Type_iVec3: {
						code += m_Code + " = " + DEF_S(_S_Cube)"(" + 输入的数据 + "); \n";
						break;
					}
					case E_值类型::e_Type_F32_1D:
					case E_值类型::e_Type_Vec3_1D:
					case E_值类型::e_Type_Vec4_1D:
						code += "if(" + 输入的数据 + "->count > 0) " + m_Code + " = " + DEF_S(_S_Cube)"(" + 输入的数据 + "->ptr_userData[0]);\n";
						code += "else " + m_Code + " = {{},{1,1,1}};\n";
						break;

					case E_值类型::e_Type_Bounding_1D:
						code += "if(" + 输入的数据 + "->count > 0) " + m_Code + " = " + "(" + 输入的数据 + "->ptr_userData[0]);\n";
						code += "else " + m_Code + " = {{},{1,1,1}};\n";
						break;
					default:
						m_Code = "";
						code = "";
						break;
				}
				break;
		}
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_包围盒插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_F32:
		case E_值类型::e_Type_UI32:
		case E_值类型::e_Type_I32:
		case E_值类型::e_Type_Vec3:
		case E_值类型::e_Type_Vec4:
		case E_值类型::e_Type_iVec3:

		case E_值类型::e_Type_Array:
		case E_值类型::e_Type_F32_1D:
		case E_值类型::e_Type_Vec3_1D:
		case E_值类型::e_Type_Vec4_1D:

		case E_值类型::e_Type_Bounding:
		case E_值类型::e_Type_Bounding_1D:
			return true;
	}
	return false;
}

void C_包围盒插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	S_文件块 文件块 = f_file_读取文件块(f);
	if (文件块.m_块类型 == E_节点工程文件块类型::e_工程文件块_插座实例属性) {
		const uint8* 下一次读取数据指针 = 文件块.m_data;
		if (f_file_版本对比(文件块, S_引擎配置::g_低版本号_0, S_引擎配置::g_文件版本号)) {
			下一次读取数据指针 = f_文件块_读数据块(下一次读取数据指针, m_默认值);
		}
	}
	free(文件块.m_data);
}

void C_包围盒插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
	auto 文件块 = f_文件块_创建(E_节点工程文件块类型::e_工程文件块_插座实例属性, S_引擎配置::g_文件版本号);
	S_UI8Array* 块 = (S_UI8Array*)f_core_array_new(0, sizeof(uint8));
	f_文件块_写数据块(块, m_默认值);
	f_file_保存文件块(f, 文件块, 块);
}








C_颜色插座::C_颜色插座(u16string name) :C_插座基类(name, E_值类型::e_Type_颜色) {
	m_默认值 = {};
	m_转换值 = {};

	m_Value = &m_默认值;
}

C_颜色插座::~C_颜色插座() {
}

void* C_颜色插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		switch (socket->m_Type) {
			case E_值类型::e_Type_颜色:
				return dynamic_cast<C_颜色插座*>(socket)->f_getData(0);
		
			case E_值类型::e_Type_Vec2:{
				auto& val = DEF_Vec2插座数据(socket, 0);
				m_转换值.x = val.x;
				m_转换值.y = val.y;
				break;
			}
			case E_值类型::e_Type_Vec4:{
				auto& val = DEF_Vec4插座数据(socket, 0);
				m_转换值 = val;
				break;
			}
			case E_值类型::e_Type_iVec2:{
				auto& val = DEF_iVec2插座数据(socket, 0);
				m_转换值.x = val.x;
				m_转换值.y = val.y;
				break;
			}
			case E_值类型::e_Type_Vec3:{
				auto& val = DEF_Vec3插座数据(socket);
				m_转换值.x = val.x;
				m_转换值.y = val.y;
				m_转换值.z = val.z;
				break;
			}
			case E_值类型::e_Type_F32:{
				auto& val = DEF_F32插座数据(socket);
				m_转换值.x = val;
				m_转换值.y = val;
				m_转换值.z = val;
				break;
			}
			case E_值类型::e_Type_I32:{
				auto& val = DEF_I32插座数据(socket);
				m_转换值.x = val;
				m_转换值.y = val;
				m_转换值.z = val;
				break;
			}
			case E_值类型::e_Type_UI32:{
				auto& val = DEF_UI32插座数据(socket);
				m_转换值.x = val;
				m_转换值.y = val;
				m_转换值.z = val;
				break;
			}
			case E_值类型::e_Type_I8:{
				auto& val = DEF_I8插座数据(socket, 0);
				m_转换值.x = val;
				m_转换值.y = val;
				m_转换值.z = val;
				break;
			}
		}
		m_Value = &m_转换值;
	}
	else if (!m_映射指针) {
		m_Value = &m_默认值;
	}
	return m_Value;
}

void C_颜色插座::f_setData(void* data, uint8 线程ID) {
	C_插座基类::f_setData(data, 线程ID);
	if (data) {
		(*m_Value) = *((vec4*)data);
	}
	else {
		m_Value = &m_默认值;
	}
}

std::string	C_颜色插座::f_构建变量代码(int32 loc) {
	std::string code;

	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_颜色) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_col";
		code = socket->f_getCode(loc);

		auto Type = socket->m_Type;
		if (socket->m_Type == E_值类型::e_Type_网格自定义属性) {
			Type = DEF_网格自定义属性插座数据(socket).m_Type;
		}


		auto* rt = m_pNode->f_getThis节点树();
		switch (rt->m_树类型) {
			case E_节点树类型::e_type_并行节点树:
			case E_节点树类型::e_type_着色节点树: {
				switch (Type) {
					case E_值类型::e_Type_F32:
					case e_Type_Vec4: {
						code = "vec4 " + m_Code + " = vec4(" + code + ");\n";
						break;
					}
				}
				break;
			}
			default:
				switch (socket->m_Type) {
					case E_值类型::e_Type_F32:
					case E_值类型::e_Type_I32:
						//code = "vec4 " + m_Code + " = " + DEF_S(_Vec4) + "(" + code + ");\n";
						//break;
					case E_值类型::e_Type_Vec2:
					case E_值类型::e_Type_iVec2:
					case E_值类型::e_Type_uVec2:
					case E_值类型::e_Type_uVec3:
					case E_值类型::e_Type_Vec3:
					case E_值类型::e_Type_Vec4: {
						code = "vec4 " + m_Code + " = " + DEF_S(_Vec4) + "(" + code + ");\n";
						//code = "ivec3 " + m_Code + " = " + DEF_S(_iVec3) + "(" + code + ");\n";
						break;
					}
					
					case E_值类型::e_Type_I8_1D:
					case E_值类型::e_Type_I32_1D:
					case E_值类型::e_Type_F32_1D:
					case E_值类型::e_Type_Vec2_1D:
					case E_值类型::e_Type_Vec3_1D:
					case E_值类型::e_Type_Vec4_1D:
						code = "if(" + code + "->count > 0) " + m_Code + " = " + DEF_S(_Vec4)"(" + code + "->ptr_userData[0]);\n";
						code += "else " + m_Code + " = {};\n";

						code = "ivec3 " + m_Code + ";\n" + code;
						break;
					default:
						m_Code = "";
						code = "";
						break;
				}
				break;
		}
		//m_pNode->m_Code += code;
		//m_pNode->m_ReleaseCode += "f_JIT_Fun_vec3_free(" + m_Code + ");\n";
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_颜色插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_网格自定义属性:

		case E_值类型::e_Type_F32:
		case E_值类型::e_Type_I8:
		case E_值类型::e_Type_I32:
		case E_值类型::e_Type_UI32:
		case E_值类型::e_Type_Vec2:
		case E_值类型::e_Type_Vec3:
		case E_值类型::e_Type_Vec4:
		case E_值类型::e_Type_iVec2:
		case E_值类型::e_Type_iVec3:
		case E_值类型::e_Type_uVec2:
		case E_值类型::e_Type_uVec3:
		case E_值类型::e_Type_Array:
		case E_值类型::e_Type_颜色:
			return true;
	}
	return false;
}

void C_颜色插座::f_upGPU块(uint8 线程ID, bool 更新) {
	C_插座基类::f_upGPU块(线程ID, 更新);

	if (m_GPU缓存.m_Mem.m_数量) {
		f_buf_Vec4_at(m_GPU缓存) = *m_Value;
	}
}

void C_颜色插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	
	S_文件块 文件块 = f_file_读取文件块(f);
	if (文件块.m_块类型 == E_节点工程文件块类型::e_工程文件块_插座实例属性) {
		const uint8* 下一次读取数据指针 = 文件块.m_data;
		if (f_file_版本对比(文件块, S_引擎配置::g_低版本号_0, S_引擎配置::g_文件版本号)) {
			下一次读取数据指针 = f_文件块_读数据块(下一次读取数据指针, m_默认值);
		}
	}
	free(文件块.m_data);
}

void C_颜色插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	auto 文件块 = f_文件块_创建(E_节点工程文件块类型::e_工程文件块_插座实例属性, S_引擎配置::g_文件版本号);
	S_UI8Array* 块 = (S_UI8Array*)f_core_array_new(0, sizeof(uint8));
	f_文件块_写数据块(块, m_默认值);
	f_file_保存文件块(f, 文件块, 块);
}
